<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\BadOpenid;

class ForbiddenOp extends RowAction
{
    
    /**
     * @return string
     */
	protected $title = '禁号';

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return \Dcat\Admin\Actions\Response
     */
    public function handle(Request $request)
    {
        // 获取 parameters 方法传递的参数
        $openid = $request->get('openid');

        BadOpenid::updateOrCreate(['openid'=>$openid],['openid'=>$openid]);
        cache()->forget("bad_openid:{$openid}");
        // 返回响应结果并刷新页面
        return $this->response()->success("该 openid 已封禁")->refresh();
    }

    /**
	 * @return string|array|void
	 */
	public function confirm()
	{
		return ['提示', '确定要封禁该 openid 吗？'];
	}

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * 设置要POST到接口的数据
     *
     * @return array
     */
    public function parameters()
    {
        return [
            'openid' => $this->row->openid,
        ];
    }
}
